package com.ruoyi.manage.cms.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.manage.cms.domain.HbJrjg;
import com.ruoyi.manage.cms.service.IHbJrjgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.ObjUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.manage.cms.domain.vo.HbJrjgVo;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 金融机构Controller
 *
 * @author wangtong
 * @date 2025-07-19
 */
@Api(tags="金融机构控制器")
@RestController
@RequestMapping("/manage/jrjg" )
public class HbJrjgController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IHbJrjgService hbJrjgService;

    /**
     * 查询金融机构列表
     */
    @ApiOperation(value = "查询金融机构",notes = "金融机构管理")
    @PreAuthorize("@ss.hasPermi('manage:jrjg:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(HbJrjgVo hbJrjg,QueryWrapper<HbJrjg> queryWrapper) {
        setQueryCondition(hbJrjg,queryWrapper);
        startPage();
        List<HbJrjg> list = hbJrjgService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(HbJrjgVo hbJrjg, QueryWrapper<HbJrjg> queryWrapper) {
        if(StrUtil.isNotBlank(hbJrjg.getName())){
            queryWrapper.like(TABLE_PREFIX+"name" ,hbJrjg.getName());
        }
        if(StrUtil.isNotBlank(hbJrjg.getServiceArea())){
            queryWrapper.eq(TABLE_PREFIX+"service_area" ,hbJrjg.getServiceArea());
        }
        if(hbJrjg.getInstitutionType() != null){
            queryWrapper.eq(TABLE_PREFIX+"institution_type" ,hbJrjg.getInstitutionType());
        }
        if(hbJrjg.getIsEnabled() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_enabled" ,hbJrjg.getIsEnabled());
        }
        if(hbJrjg.getIsDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_deleted" ,hbJrjg.getIsDeleted());
        }
        if (ObjUtil.isNotNull(hbJrjg.getBeginPublishTime()) && ObjUtil.isNotNull(hbJrjg.getEndPublishTime())){
            queryWrapper.between(TABLE_PREFIX+"publish_time", DateUtil.beginOfDay(hbJrjg.getBeginPublishTime()),DateUtil.endOfDay(hbJrjg.getEndPublishTime()));
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出金融机构列表
     */
    @PreAuthorize("@ss.hasPermi('manage:jrjg:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, HbJrjgVo hbJrjg,QueryWrapper<HbJrjg> queryWrapper) {
        if (CollUtil.isEmpty( hbJrjg.getIds())){
            setQueryCondition(hbJrjg,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",hbJrjg.getIds());
        }
        List<HbJrjg> list = hbJrjgService.list(queryWrapper);
        ExcelUtil<HbJrjg> util = new ExcelUtil<HbJrjg>(HbJrjg. class);
        util.exportExcel(response,list, "jrjg");
    }

    /**
     * 获取金融机构详细信息
     */
    @ApiOperation(value = "获取金融机构",notes = "金融机构管理")
    @PreAuthorize("@ss.hasPermi('manage:jrjg:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(hbJrjgService.queryGetById(id));
    }

    /**
     * 新增金融机构
     */
    @ApiOperation(value = "新增金融机构",notes = "金融机构管理")
    @PreAuthorize("@ss.hasPermi('manage:jrjg:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HbJrjg hbJrjg) {
        return toAjax(hbJrjgService.addHbJrjg(hbJrjg) ? 1 : 0);
    }

    /**
     * 修改金融机构
     */
    @ApiOperation(value = "修改金融机构",notes = "金融机构管理")
    @PreAuthorize("@ss.hasPermi('manage:jrjg:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody HbJrjg hbJrjg) {
        return toAjax(hbJrjgService.updateHbJrjg(hbJrjg) ? 1 : 0);
    }

    /**
     * 删除金融机构
     */
    @ApiOperation(value = "删除金融机构",notes = "金融机构管理")
    @PreAuthorize("@ss.hasPermi('manage:jrjg:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hbJrjgService.deleteHbJrjgByIds(ids) ? 1 : 0);
    }
}
